

using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Abp.Linq;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.UI;
using Abp.Domain.Repositories;
using Abp.Domain.Services;

using Yozeev;
using Yozeev.BusinessLogic;
using Abp.Authorization.Users;

namespace Yozeev.BusinessLogic.DomainService
{
    /// <summary>
    /// TicketScheduleEnable领域层的业务管理
    ///</summary>
    public class TicketScheduleEnableManager :YozeevDomainServiceBase, ITicketScheduleEnableManager
    {

		private readonly IRepository<TicketScheduleEnable, Guid> _repository;
		private readonly IRepository<TicketRole, Guid> _ticketRoleRepository;
		private readonly IRepository<UserRole, long> _userRoleRepository;

		/// <summary>
		/// TicketScheduleEnable的构造方法
		///</summary>
		public TicketScheduleEnableManager(
			IRepository<TicketScheduleEnable, Guid> repository
			, IRepository<TicketRole, Guid> ticketRoleRepository
			, IRepository<UserRole, long> userRoleRepository
		)
		{
			_repository =  repository;
			_ticketRoleRepository = ticketRoleRepository;
			_userRoleRepository = userRoleRepository;
		}


		/// <summary>
		/// 初始化
		///</summary>
		public void InitTicketScheduleEnable()
		{
			throw new NotImplementedException();
		}

		// TODO:编写领域业务代码

		public List<TicketScheduleEnable> GetList(Guid scheduleId)
		{
			return _repository.GetAllList(m => m.ScheduleId == scheduleId);
		}

		/// <summary>
		/// 根据用户Id查询航班可售票
		/// </summary>
		/// <param name="userId"></param>
		/// <param name="expression"></param>
		/// <returns></returns>
		public async Task<List<TicketScheduleEnable>>  GetListByUserId(long userId ,Expression<Func<TicketScheduleEnable, bool>> expression)
		{
			//Expression<Func<TicketScheduleEnable, bool>> expression = entity => 1 == 1;
			//var query = await _repository.GetAllListAsync(expression);
			var query = from ticketSchedule in _repository.GetAll().Where(expression)
					   join ticketRole in _ticketRoleRepository.GetAll() on ticketSchedule.TicketPriceId equals ticketRole.TicketPriceId
					   join roleUser in _userRoleRepository.GetAll().Where(m=>userId==m.UserId) on ticketRole.RoleId equals roleUser.RoleId
					   select new { ticketSchedule ,
						   ticketSchedule.Schedule,
						   ticketSchedule.Schedule.Boat,
						   ticketSchedule.Schedule.Route,
						   ticketSchedule.TicketPrice,
						   ticketSchedule.TicketPrice.Ticket,
					   };
			var list = new List<TicketScheduleEnable>();
			foreach(var entity in query)
			{
				var temp = entity.ticketSchedule;
				temp.Schedule = entity.Schedule;
				temp.Schedule.Boat = entity.Boat;
				temp.Schedule.Route = entity.Route;
				temp.TicketPrice = entity.TicketPrice;
				temp.TicketPrice.Ticket = entity.Ticket;
				list.Add(temp);
			}
			return list.ToList();
		}
		 

	}
}
